<?php
namespace app\shop\controller;
use x\my\Captcha;
use x\Xbase;
class Publics extends Base{

    //获取子地区名称
    public function getComAddress(){
        $pid = $this->post('pid') ? $this->post('pid') : $this->get('pid');
        $rs = $this->db->query('select id,name from com_address where parent_id=?',[$pid]);
        suc($rs);
    }

    //获取子地区名称
    public function getAllAddress(){
        $rs = $this->db->query('select id,name from com_address');
        $map = [];
        foreach ($rs as $key => $value) {
            $map[$value['id']] = $value['name'];
        }
        return $map;
    }


    //登录界面
    public function login(){
        if(is_post()){
            $d = $this->post;
            $token = cookie('captcha_token');
            if( !Captcha::checkCaptcha($token,$d['code']) ){
                cookie('js_name','code');
                err('验证码错误!');
            }
            $rule = [
                'username'=>'require',
                'password'=>'require',
            ];
            $msg = [
                'username.require' => '请输入用户名',
                'password.require' => '请输入密码',
            ];
            check($rule,$msg,$d);
            $rs = m('Shop','c')->login($d['username'],$d['password']);
            if($rs){
                cookie('js_url',url('index'));
                cookie('js_msg','登录成功!');
                return suc();
            }
            err(m('User','c')->getError());
        }
        $this->fetch([],'',false);
    }

    //验证码
    public function captcha(){
        $token = token();
        cookie('captcha_token',$token);
        return Captcha::generate($token);
    }
    //退出
    public function logout(){
        $token = cookie('token');
        if($token){
            $this->db->update('shop',['token'=>''],['token=?',[$token]]);
        }
        cookie(null,null);
        r(url('publics/login'));
    }
}
?>
